package com.Demo;

//图片作为按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL;

//按钮
public class JButtonDemo extends JFrame {
    public JButtonDemo() throws HeadlessException {
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL resource = JButtonDemo.class.getResource("carrot.jpg");
        Icon icon = new ImageIcon(resource);

        //把这个图片放在按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");

        //add
        container.add(button);

        this.setVisible(true);
        this.setBounds(400,400,300,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JButtonDemo();
    }
}
